/*
 * @(#)Basket.java   09/12/30
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



package Example.Components;

import Example.Interfaces.Account;
import Example.Interfaces.AccountOprs;
import Example.Interfaces.Bike;
import Example.Interfaces.BikeOprs;
import Example.Interfaces.CourseOprs;

import org.objectweb.fractal.api.control.BindingController;

import java.util.Vector;

//~--- classes ----------------------------------------------------------------

/**
 * Class description
 *
 *
 * @version        1.0, 09/12/30
 * @author         Huaxi (Yulin) ZHANG    
 */
public class Basket implements Account, Bike, BindingController {

    /** Field description */
    private AccountOprs AccountOprs;

    /** Field description */
    private BikeOprs BikeOprs;

    /** Field description */
    private CourseOprs CourseOprs;

    /**
     * Method description
     *
     * @param userID  
     * @param userPassword  
     * @return  
     */
    public Object[] login(int userID, String userPassword) {
        System.out.println("basket login");

        Object[] userInforA  = null;
        Object[] userInforC  = null;
        Object   userInfor[] = new Object[10];

        userInforA = AccountOprs.checkID(userID, userPassword);

        for (int i = 0; i < userInforA.length; i++) {
            userInfor[i] = userInforA[i];
        }

        userInforC = CourseOprs.startCourse(userID);

        for (int i = 1; i < userInforC.length; i++) {
            userInfor[i + 5] = userInforC[i];

            // System.out.println("ee"+userInfor[i+5]);
        }

        return userInfor;
    }

    /**
     * Method description
     *
     * @param userID  
     * @param cost  
     * @return  
     */
    public double pay(int userID, double cost) {
        return AccountOprs.debit(userID, cost);
    }

    /**
     * Method description
     *
     * @param locationStart  
     * @param locationEnd  
     * @return  
     */
    public Vector findBike(String locationStart, String locationEnd) {
        return BikeOprs.selectBike(locationStart, locationEnd);
    }

    /**
     * Method description
     *
     * @param bikeID  
     * @param userID  
     * @return  
     */
    public boolean rentB(int bikeID, int userID) {
        return BikeOprs.rentBike(bikeID, userID);
    }

    /**
     * Method description
     *
     * @param userID  
     * @param bikeID  
     * @param location  
     * @param trip  
     * @return  
     */
    public double returnB(int userID, int bikeID, String location, double trip) {
        BikeOprs.returnBike(bikeID, location, trip);

        String rentT = CourseOprs.endCourse(userID, bikeID);
        long   t1    = Long.parseLong(rentT);
        long   t2    = System.currentTimeMillis();
        double t     = (t2 - t1) / 1000 / 60 * 0.1;

        return 0.0;
    }

    // the component interfaces required

    /**
     * Method description
     *
     * @return  
     */
    public String[] listFc() {
        return new String[] { "AccountOprs,BikeOprs,CourseOprs" };
    }

    /**
     * Method description
     *
     * @param cItf  
     * @return  
     */
    public Object lookupFc(String cItf) {
        if (cItf.equals("CourseOprs")) {
            return CourseOprs;
        }

        if (cItf.equals("BikeOprs")) {
            return BikeOprs;
        }

        if (cItf.equals("AccountOprs")) {
            return AccountOprs;
        }

        return null;
    }

    /**
     * Method description
     *
     * @param cItf  
     * @param sItf  
     */
    public void bindFc(String cItf, Object sItf) {
        if (cItf.equals("CourseOprs")) {
            CourseOprs = (CourseOprs) sItf;
        }

        if (cItf.equals("BikeOprs")) {
            BikeOprs = (BikeOprs) sItf;
        }

        if (cItf.equals("AccountOprs")) {
            AccountOprs = (AccountOprs) sItf;
        }
    }

    /**
     * Method description
     *
     * @param cItf  
     */
    public void unbindFc(String cItf) {
        if (cItf.equals("CourseOprs")) {
            CourseOprs = null;
        }

        if (cItf.equals("BikeOprs")) {
            BikeOprs = null;
        }

        if (cItf.equals("AccountOprs")) {
            AccountOprs = null;
        }
    }
}
